package com.spyfool.aopofaspectj.aspect;

import android.util.Log;

import com.spyfool.aopofaspectj.aspect.anno.BehaviorTrace;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * 行为记录
 */
@Aspect
public class BehaviorTraceAspect {
    private static final String TAG = "BehaviorTraceAspect";

    @Pointcut("execution(@com.spyfool.aopofaspectj.aspect.anno.BehaviorTrace * *(..))")
    public void behaviorHandle() {
    }

    @Around("behaviorHandle()")
    public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature=(MethodSignature)joinPoint.getSignature();
        String className=methodSignature.getDeclaringType().getSimpleName();
        String methodName=methodSignature.getName();
        String funName=methodSignature.getMethod().getAnnotation(BehaviorTrace.class).value();

        long begin = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long duration = System.currentTimeMillis() - begin;
        Log.d(TAG, String.format("功能：%s,%s类的%s方法执行了，用时%d ms", funName, className, methodName, duration));
        return proceed;
    }
}
